package com.alibaba.cloud.cargo.ui;

import com.alibaba.cloud.cargo.application.command.CargoCmdService;
import com.alibaba.cloud.cargo.application.command.cmd.CargoBookCommand;
import com.alibaba.cloud.cargo.application.command.cmd.CargoDeleteCommand;
import com.alibaba.cloud.cargo.application.command.cmd.CargoDeliveryUpdateCommand;
import com.alibaba.cloud.cargo.application.command.cmd.CargoSenderUpdateCommand;
import com.alibaba.cloud.cargo.application.query.CargoQueryService;
import com.alibaba.cloud.cargo.application.query.dto.CargoDTO;
import com.alibaba.cloud.cargo.application.query.qry.CargoFindbyCustomerQry;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author curise
 */
@RestController
@RequestMapping("/cargo")
@RequiredArgsConstructor
public class CargoController {

    private final CargoQueryService cargoQueryService;
    private final CargoCmdService cargoCmdService;

    @GetMapping
    public List<CargoDTO> queryCargos(@RequestParam(value = "phone", required = false) String phone) {
        if (!StringUtils.isEmpty(phone)) {
            CargoFindbyCustomerQry qry = new CargoFindbyCustomerQry();
            qry.setCustomerPhone(phone);
            return cargoQueryService.queryCargos(qry);
        }
        return cargoQueryService.queryCargos();
    }

    @GetMapping(value = "/{cargoId}")
    public CargoDTO cargo(@PathVariable String cargoId) {
        return cargoQueryService.getCargo(cargoId);
    }

    @PostMapping
    public void book(@RequestBody CargoBookCommand cargoBookCommand) {
        cargoCmdService.bookCargo(cargoBookCommand);
    }

    @PutMapping(value = "/{cargoId}/delivery")
    public void modifyDestinationLocationCode(@PathVariable String cargoId,
                                              @RequestBody CargoDeliveryUpdateCommand cmd) {
        cmd.setCargoId(cargoId);
        cargoCmdService.updateCargoDelivery(cmd);
    }

    @PutMapping(value = "/{cargoId}/sender")
    public void modifySender(@PathVariable String cargoId, @RequestBody CargoSenderUpdateCommand cmd) {
        cmd.setCargoId(cargoId);
        cargoCmdService.updateCargoSender(cmd);
    }

    @DeleteMapping(value = "/{cargoId}")
    public void remoteCargo(@PathVariable String cargoId) {
        CargoDeleteCommand cmd = new CargoDeleteCommand();
        cmd.setCargoId(cargoId);
        cargoCmdService.deleteCargo(cmd);
    }
}
